package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.common.constant.Dict;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.persistence.dao.OpMessageMapper;
import com.stylefeng.guns.common.persistence.dao.OrgUserMapper;
import com.stylefeng.guns.common.persistence.model.OpMessage;
import com.stylefeng.guns.common.persistence.model.OrgUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;
/**
 * 消息通知表控制器
 *
 * @author fengshuonan
 * @Date 2018-12-27 16:10:48
 */
@Controller
@RequestMapping("/opMessage")
public class OpMessageController extends BaseController {

    private String PREFIX = "/system/opMessage/";
    @Autowired
    OpMessageMapper opMessageMapper;
    @Autowired
    OrgUserMapper orgUserMapper;
    /**
     * 跳转到消息通知表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "opMessage.html";
    }

    /**
     * 跳转到添加消息通知表
     */
    @RequestMapping("/opMessage_add")
    public String opMessageAdd() {
        return PREFIX + "opMessage_add.html";
    }

    /**
     * 跳转到修改消息通知表
     */
    @RequestMapping("/opMessage_update/{opMessageId}")
    public String opMessageUpdate(@PathVariable Integer opMessageId, Model model) {
        OpMessage old=this.opMessageMapper.selectById(opMessageId);
       Integer a = old.getUserId();
        OrgUser orgUser = orgUserMapper.selectById(a);
        String userName = orgUser.getUserName();
        old.setUserName(userName);
        model.addAttribute("opMessage",old);
        return PREFIX + "opMessage_edit.html";
    }

    /**
     * 获取消息通知表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<OpMessage> opMessages = opMessageMapper.selectList(null);
        for(OpMessage opMessage:opMessages){
            OrgUser orgUser = orgUserMapper.selectById(opMessage.getUserId());
            opMessage.setUserName(orgUser.getUserName());
            Integer b = Integer.valueOf(opMessage.getIsRead());
            if(b == 1){
                opMessage.setIsRead("已读");
            }else if(b == 0){
                opMessage.setIsRead("未读");
            }else{
                opMessage.setIsRead("兔子追月亮了");
            }
        }
        return opMessages;
    }

    /**
     * 新增消息通知表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(OpMessage opMessage) {
        if(opMessage!=null){
            opMessage.setCreateTime(new Date());
        }
        opMessage.setIsRead("0");
        Integer insert = opMessageMapper.insert(opMessage);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除消息通知表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除节点",key = "opMessageId",dict = Dict.DeleteDict)
    public Object delete(@RequestParam Integer opMessageId) {
        this.opMessageMapper.deleteById(opMessageId);
        return SUCCESS_TIP;
    }


    /**
     * 修改消息通知表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改节点",key = "opMessageId",dict = Dict.DictMap)
    public Object update(OpMessage opMessage) {
        Integer update = opMessageMapper.updateById(opMessage);
        return super.SUCCESS_TIP;
    }

    /**
     * 消息通知表详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
